package com.qf.Controller;

import com.qf.Servlet.Impl.UserServiceImpl;
import com.qf.pojo.Profile;
import com.qf.pojo.User;
import com.qf.utils.Constants;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

/**
 * @author Wy
 * @version V1.0
 * @Project web人员管理系统
 * @Package com.qf.Controller
 * @Description:
 * @Date 2022/5/27 12:29
 */
@WebServlet("/login")
public class Usercontroller extends  BaseServlet {


    public  String register(HttpServletRequest request,HttpServletResponse response) throws InvocationTargetException, IllegalAccessException {
        Map<String, String[]> parameterMap = request.getParameterMap();
        User user = new User();
        BeanUtils.populate(user,parameterMap);
        //3. 调用service, 将用户对象保存到mysql数据库的用户表中
        UserServiceImpl userService = new UserServiceImpl();
        int i = userService.AddUser(user);
        if(i>0){
            return  "redirect:"+request.getContextPath()+"/registerSuccess.jsp";
        }else {
            return  "redirect:"+request.getContextPath()+"/register.jsp";
        }



    }

    /**
     * 用户注册，检验用户是否可用
     * @param request
     * @param response
     * @throws IOException
     */


    public  void check(HttpServletRequest request,HttpServletResponse response) throws IOException {
        String username = request.getParameter("username");
        UserServiceImpl userService = new UserServiceImpl();
        boolean b = userService.checkduUser(username);
        //3.将信息传递给页面
        if(b){
            response.getWriter().write(Constants.NOT_HAS_USER);

        }else {
            response.getWriter().write(Constants.HAS_USER);
        }
    }

    public  String login(HttpServletRequest request, HttpServletResponse response){
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        if(username==null || password==null){
            return  "redirect:"+request.getContextPath()+"/logins.jsp";
        }
        UserServiceImpl userService = new UserServiceImpl();
        User login = userService.login(username, password);
        if(login!=null){
            request.getSession().setAttribute("admin",login);
            return "redirect:/user?method=login";
        }else {
            //用户名, 密码不正确, 跳转到登录页面重新登录
            return  "redirect:"+request.getContextPath()+"/logins.jsp";
        }


    }
    /**
     * 把数据库里面的商品展示在界面
     * @param request
     * @param response
     * @return
     */






}
